package com.wwwarehouse.contract.contract.modify;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.InfoCardOrderDetailBean;
import com.wwwarehouse.contract.common.ContractCommon;
import com.wwwarehouse.contract.contract.MyBaseViewPagerFragment;
import com.wwwarehouse.contract.contract_address.ChooseAddressViewPagerFragment;
import com.wwwarehouse.contract.core.BaseViewPagerFragment;
import com.wwwarehouse.contract.core.BindEventBus;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.core.ContractEventCode;
import com.wwwarehouse.contract.core.EventManager;
import com.wwwarehouse.contract.event.ChooseAddressEvent;
import com.wwwarehouse.contract.event.ContractEvent;
import com.wwwarehouse.contract.event.DeleteIssuedGoodsEvent;
import com.wwwarehouse.contract.event.RefreshConfirmOrderEvent;
import com.wwwarehouse.contract.event.RefreshPublishGoodsEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/11/8.
 * 修改发布供应 已发布商品
 */
public class IssuedGoodsViewPagerFragment extends CommonBasePagerFragment {
	private String mContractUkid;
	private String mSupplierBusinessId;
	private InfoCardOrderDetailBean.ItemDetailBean mItemDetailBean;
	boolean isShow;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		EventBus.getDefault().register(this);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof IssuedGoodsViewPagerFragment) {
			mActivity.setTitle(R.string.string_contract_fff_modify_issued_goods_title);
		}
	}

	@Override
	public void requestDatas() {
		super.requestDatas();
		mConfirmBtn.setVisibility(View.VISIBLE);
		mConfirmBtn.setText(R.string.string_contract_published_goods_confirm_btn_text);
		if (peekFragment() instanceof IssuedGoodsViewPagerFragment) {
			showCustomBtn("新增商品");
		}
		((BaseCardDeskActivity)mActivity).setTitleBt1Enable(true);
		if (getArguments() == null || StringUtils.isNullString(getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID))
				|| StringUtils.isNullString(getArguments().getString(ContractConstant.KEY_SUPPLIER_BUSINESSID
		))) {
			mStateLayout.showEmptyView(true);
			mStateLayout.setEmptyListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					requestDatas();
				}
			});
			return;
		}
		mContractUkid = getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID);
		mSupplierBusinessId = getArguments().getString(ContractConstant.KEY_SUPPLIER_BUSINESSID);
		mConfirmBtn.setEnabled(true);
		mConfirmBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

//				Map<String, Object> map = new HashMap<>();
//				map.put("pbUkid", pbUkid);
//				httpPost(ContractConstant.PUBLISHRESOURCEITEM, map, 0, true, "");
				popFragment();
				RefreshPublishGoodsEvent publishingRulesEvent = new RefreshPublishGoodsEvent("success");
				EventBus.getDefault().post(publishingRulesEvent);

			}
		});


		Map<String, Object> map = new HashMap<>();
		map.put("page", "1");
		map.put("contractUkid", mContractUkid);
		map.put("size", 9999);
		loadData(ContractConstant.METHOD_ORDER_DETAILS_LIST, map, 1);
	}

	@Override
	public void onRefreshListener() {
		requestDatas();
	}

	@Override
	public void onLoadSuccess(String data, int what) {
		if (what == 1) {
			InfoCardOrderDetailBean detailBean = JSON.parseObject(data, InfoCardOrderDetailBean.class);
			if (detailBean == null || detailBean.getList() == null || detailBean.getList().size() == 0) {
				mStateLayout.showEmptyView(true);
				mActivity.setTitle(R.string.string_contract_fff_modify_issued_goods_title);
				mStateLayout.setEmptyListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
				hideConfirmButton();
				return;
			}
			showConfirmButton();
			mActivity.setTitle("已发布商品("+detailBean.getList().size() + ")");

			try {
				setData(detailBean.getList().size(), 5, IssuedGoodsFragment.class.getName(), null, detailBean.getList());
			} catch (Exception e) {
				e.printStackTrace();
			}

		} else if (what == 2) {
			dismissProgressDialog();
			requestDatas();
		}
	}

	@Override
	public void onLoadError(String errorInfo) {

	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

	}


	@Override
	public void titleBt1Click() {
		if (peekFragment() instanceof IssuedGoodsViewPagerFragment) {
			if (isShow) {
				((BaseCardDeskActivity) mActivity).hideTitleBt();
			}
			isShow = !isShow;
			Bundle bundle = new Bundle();
			bundle.putString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID, mContractUkid);
			bundle.putString(ContractConstant.KEY_SUPPLIER_BUSINESSID, mSupplierBusinessId);
			ChooseGoodsViewPagerFragment chooseGoodsViewPagerFragment = new ChooseGoodsViewPagerFragment();
			chooseGoodsViewPagerFragment.setArguments(bundle);
			pushFragment(chooseGoodsViewPagerFragment);
		}
	}

	public void onEventMainThread(ContractEvent contractEvent) {
		if (peekFragment() instanceof IssuedGoodsViewPagerFragment) {
			if (contractEvent.getData() == null) {
				return;
			}
			if (contractEvent.getCode() == ContractEventCode.CODE_REFRESH_ISSUED_GOODS) {
				requestDatas();
			} else if (contractEvent.getCode() == ContractEventCode.CODE_DELETE_ISSUED_GOODS) {
				//删除条目
				DeleteIssuedGoodsEvent deleteIssuedGoodsEvent = (DeleteIssuedGoodsEvent) contractEvent.getData();
				if (deleteIssuedGoodsEvent == null || StringUtils.isNullString(deleteIssuedGoodsEvent.getItemPublishUkid())) {
					return;
				}
				Map<String, Object> mParamsMap = new HashMap<>();
				mParamsMap.put("itemPublishUkid", deleteIssuedGoodsEvent.getItemPublishUkid());
				isFirst = false;
				showProgressDialog();
				loadData(ContractConstant.METHOD_DELETE_RESOURCE_ITEM_PUBLISHED, mParamsMap, 2);
			}
		}
	}

	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (peekFragment() instanceof IssuedGoodsViewPagerFragment && backListenerEvent.getMsg().equals("IssuedGoodsViewPagerFragment")) {
			popFragment();
			EventBus.getDefault().post(new RefreshPublishGoodsEvent("refresh"));
		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
}
